package com.company.project.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.net.URISyntaxException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * http请求工具类
 *
 * @since 2020-03-25
 */

@Slf4j
public class HttpClientUtils {

    private static final Logger LOG = LoggerFactory.getLogger(HttpClientUtils.class);

    /**
     * get请求
     *
     * @param url    url
     * @param params 请求参数
     */
    public static String get(String url, MultiValueMap<String, String> params) {
        return get(url, params, null);
    }

    /**
     * get请求
     *
     * @param url     url
     * @param params  请求参数
     * @param headers 请求头
     */
    public static String get(String url, MultiValueMap<String, String> params, MultiValueMap<String, String> headers) {
        return request(url, params, headers, HttpMethod.GET);
    }

    /**
     * post请求
     *
     * @param url    url
     * @param params 请求参数
     */
    public static String post(String url, MultiValueMap<String, String> params) {
        return post(url, params, null);
    }

    /**
     * post请求
     *
     * @param url     url
     * @param params  请求参数
     * @param headers 请求头
     */
    public static String post(String url, MultiValueMap<String, String> params, MultiValueMap<String, String> headers) {
        return request(url, params, headers, HttpMethod.POST);
    }

    /**
     * put请求
     *
     * @param url    url
     * @param params 请求参数
     */
    public static String put(String url, MultiValueMap<String, String> params) {
        return put(url, params, null);
    }

    /**
     * put请求
     *
     * @param url     url
     * @param params  请求参数
     * @param headers 请求头
     */
    public static String put(String url, MultiValueMap<String, String> params, MultiValueMap<String, String> headers) {
        return request(url, params, headers, HttpMethod.PUT);
    }

    /**
     * delete请求
     *
     * @param url    url
     * @param params 请求参数
     */
    public static String delete(String url, MultiValueMap<String, String> params) {
        return delete(url, params, null);
    }

    /**
     * delete请求
     *
     * @param url     url
     * @param params  请求参数
     * @param headers 请求头
     */
    public static String delete(String url, MultiValueMap<String, String> params, MultiValueMap<String, String> headers) {
        return request(url, params, headers, HttpMethod.DELETE);
    }

    /**
     * 表单请求
     *
     * @param url     url
     * @param params  请求参数
     * @param headers 请求头
     * @param method  请求方式
     */
    public static String request(String url, MultiValueMap<String, String> params, MultiValueMap<String, String> headers, HttpMethod method) {
        if (params == null) {
            params = new LinkedMultiValueMap<>();
        }
        return request(url, params, headers, method, MediaType.APPLICATION_FORM_URLENCODED);
    }

    /**
     * http请求
     *
     * @param url       url
     * @param params    请求参数
     * @param headers   请求头
     * @param method    请求方式
     * @param mediaType 参数类型
     */
    public static String request(String url, Object params, MultiValueMap<String, String> headers, HttpMethod method, MediaType mediaType) {
        long startTime = System.currentTimeMillis();
        if (url == null || url.trim().isEmpty()) {
            return null;
        }
        //复杂构造函数的使用
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(10000);// 设置超时
        requestFactory.setReadTimeout(10000);
        RestTemplate client = new RestTemplate(requestFactory);
        // header
        HttpHeaders httpHeaders = new HttpHeaders();
        if (headers != null) {
            httpHeaders.putAll(headers);
        }
        // 提交方式：表单、json
        httpHeaders.setContentType(mediaType);
        HttpEntity<Object> httpEntity = new HttpEntity<>(params, httpHeaders);
        ResponseEntity<String> response = client.exchange(url, method, httpEntity, String.class);
//        System.out.println("code:" + response.getStatusCode());
//        System.out.println("body:" + response.getBody());
        log.error("本次请求: " + url + " 耗时:" + (System.currentTimeMillis() - startTime) + " ms");
        return response.getBody();
    }


    /**
     * http请求
     *
     * @param url    url
     * @param params 请求参数
     */
    public static String requestIM(String url, Object params) {
        if (url == null || url.trim().isEmpty()) {
            return null;
        }
        RestTemplate client = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<Object> httpEntity = new HttpEntity<>(params, httpHeaders);
        ResponseEntity<String> response = client.exchange(url, HttpMethod.POST, httpEntity, String.class);
        System.out.println("code:" + response.getStatusCode());
        System.out.println("body:" + response.getBody());
        return response.getBody();
    }


    public static void main(String[] args) throws URISyntaxException {

//        Map<String, Object> params = new HashMap<>();
//        params.put("source", "qingying");
//        params.put("hospitalId", "1111");
//        params.put("AppOrderId", "2222");
//        URIBuilder builder = new URIBuilder("fuji.com");
//        Set<String> keySet = params.keySet();
//        for (String key : keySet) {
//            String value = params.get(key).toString();
//            builder.addParameter(key, value);
//        }
////        URI uri = builder.build();
////        System.out.println(uri);
//
//        String result = HttpClientUtils.get("https://testeasyimageapi.efilmcloud.com:8905/Center/Intergration/Study/VisitStatus?source=qingying&appOrderId=Wechat10122020040169800LXR56520720200526180141897&orderId=P0020200526180142000017&payTime=2020/05/26 17:32:09&hospitalId=1012&accNum=XR565207&patientId=2020040169800", null, null);
//        System.out.println(result);
//        if (result.equals("ERROR")) {
//            System.out.println("API请求失败");
//
//        }
    }

}
